const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatDate2(value) {
  var date = new Date(value);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  var d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  var h = date.getHours();
  h = h < 10 ? ('0' + h) : h;
  var minute = date.getMinutes();
  var second = date.getSeconds();
  minute = minute < 10 ? ('0' + minute) : minute;
  second = second < 10 ? ('0' + second) : second;
  return y + '-' + m + '-' + d //+ ' ' + h + ':' + minute;// + ':' + second;
}

const formatDate = (dates, type) => {
  let time;
  if(type == 0){
    // 下一天
    time = (dates).getTime() + 24 * 60 * 60 * 1000;
  } else if(type == 1){
    // 当前
    time = (dates).getTime();
  } else if(type == 2){
    // 前一天
    time = (dates).getTime() - 24 * 60 * 60 * 1000;
  }
  var date = new Date(time); // 获取的是前一天日期
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  month = month < 10 ? ('0' + month) : month;
  let day = date.getDate();
  day = day < 10 ? ('0' + day) : day;
  let str = year + "-" + month + "-" + day;
  return str
}

const getDifferDay = (date1, date2) => {
  let startDate = new Date(date1);
  let endDate = new Date(date2);
  var date = (startDate.getTime() - endDate.getTime()) / (1000 * 60 * 60 * 24);
  return date;
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

module.exports = {
  formatTime: formatTime,
  formatDate: formatDate,
  getDifferDay: getDifferDay,
  formatDate2: formatDate2
}
